#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sr.h"
#include <assert.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include "foo.h"

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
	char tsUnit[][50] = {
		"canny",             //0 
		"downUp",            //1        
		"roi",               //2          
		"hough",             //3             
		"threshold",         //4  
		"squares",           //5        
		"HSV",               //6         
		"CannyHough",        //7         
		"otsu",              //8
		"save",              //9
		"findFile",          //10
		"diejia",            //11
		"findRect",          //12
		"liantong",          //13
		"begin",             //14
		"ruihua"            //15

	};
	if (argc == 1) {
		return 0;
	}
	char *name = (char *) malloc (sizeof(char) * 256);
	memset(name, 0, sizeof(char) * 256);
	strcpy(name, (char*) "res/temp/capture00.bmp");
	if (argc > 2) {
		strcpy(name, argv[2]);
	}
	IplImage *src = cvLoadImage(name);
	assert(src);
	if (strcmp(argv[1], tsUnit[0]) == 0) {
		strcpy(name, (char *)"res/temp/capture04.bmp");
		if (argc > 2) {
			strcpy(name, argv[2]);
		}
		cvReleaseImage(&src);
		IplImage *src = cvLoadImage(name);
		assert(src);
		IplImage *dst = cvCreateImage(
				cvSize(src->width, src->height),
				IPL_DEPTH_8U,
				1
				);
		//src = colorToBlack(src);
		//dst = colorToBlack(dst);
		cvCvtColor(src, dst, CV_RGB2GRAY);
		canny(dst, dst, 150, 201, 3);
		sobel(dst, dst, 2, 0, 11);
		src = smooth(dst, dst, 0, 3, 0, 0., 0.);
		cvDilate(dst, dst, NULL, 1);
		cvErode(dst, dst, NULL, 1);
		cvNamedWindow("win_1");
		cvShowImage("win_1", dst);
		cvSaveImage("res/temp/ts.png", dst);
		cvWaitKey(0);
		findRect(0, NULL);
		cvDestroyWindow("win_1");
	}
	if (strcmp(argv[1], tsUnit[1]) == 0) {
		IplImage *dst = cvCreateImage(
				cvSize(src->width / 2, src->height / 2),
				IPL_DEPTH_8U,
				src->nChannels
				);
		pyDown(src, dst);
		cvReleaseImage(&src);
		src = cvCreateImage(
				cvSize(dst->width * 2, dst->height * 2),
				IPL_DEPTH_8U,
				dst->nChannels
				);
		pyUp(dst, src);
	}
	if (strcmp(argv[1], tsUnit[2]) == 0) {
		int argc = 3;
		char *argv[] = {(char *)"main", (char *)"res/temp/ts.png", (char *)"res/temp/chepai.png"};
		_roi(argc, argv);
	}
	if (strcmp(argv[1], tsUnit[3]) == 0) {
		Mat src = imread(name);
		Mat dst;
		Mat cdst;
		houghLine(src, dst, cdst);
	}
	if (strcmp(argv[1], tsUnit[4]) == 0) {
		IplImage *src = cvLoadImage("res/temp/ts.png");
		threshold(src);
	} 
	if (strcmp(argv[1], tsUnit[5]) == 0) {
		IplImage *dst = cvCreateImage(
				cvGetSize(src),
				IPL_DEPTH_8U,
				1
				);
		cvCvtColor(src, dst, CV_RGB2GRAY);
		findRect(0, NULL);
	}
	if (strcmp(argv[1], tsUnit[6]) == 0) {
		HSV(0, NULL);
	}
	if (strcmp(argv[1], tsUnit[7]) == 0) {
		CannyHough(0, NULL);
	}
	if (strcmp(argv[1], tsUnit[8]) == 0) {
		IplImage *dst = cvCreateImage(
				cvSize(src->width, src->height),
				IPL_DEPTH_8U,
				1
				);
		cvCvtColor(src, dst, CV_RGB2GRAY);
		int threshold = otsu(dst);
	}
	if (strcmp(argv[1], tsUnit[9]) == 0) {
		char **name = (char **) malloc (sizeof(char *) * 256);
		char *tmp = (char *) malloc (sizeof(char) * 256);
		int i;
		for (i = 0; i < 15; ++ i) {
			sprintf(tmp, "%02d.bmp", i);
			name[i] = (char *) malloc (sizeof(char) * 256);
			strcpy(name[i], "res/temp/capture");
			strcat(name[i], tmp);
		}
		outFindResult(name, i - 1);
	}
	if (strcmp(argv[1], tsUnit[10]) == 0) {
		int cnt = 0;
		char **result = findFile((char *)"res/temp", cnt);
		for (int i = 0; i < cnt; ++ i) {
			printf("%s\n", result[i]);
		}
	}
	if (strcmp(argv[1], tsUnit[11]) == 0) {
		int cnt = 0;
		char **imgSet = findFile((char *)"res/result/", cnt);
		assert(imgSet[1]);
		diejia(imgSet, cnt);
	}
	if (strcmp(argv[1], tsUnit[12]) == 0) {
		findRect(0, 0);
	}
	if (strcmp(argv[1], tsUnit[13]) == 0) {
		assert(src);
		liantong(&src);
	}
	if (strcmp(argv[1], tsUnit[14]) == 0) {
		king_lk();
	}
	if (strcmp(argv[1], tsUnit[15]) == 0) {
	}

	return 0;
}
